package com.dropdb.demo.service.impl;

import com.dropdb.demo.bean.vo.BaseData;
import com.dropdb.demo.bean.Comment;
import com.dropdb.demo.bean.CommentExample;
import com.dropdb.demo.bean.bo.AdminListCommentsBO;
import com.dropdb.demo.bean.vo.CommentCountData;
import com.dropdb.demo.bean.vo.CommentListData;
import com.dropdb.demo.bean.vo.CommentListElementData;
import com.dropdb.demo.mapper.CommentMapper;
import com.dropdb.demo.mapper.CommentMultiTableMapper;
import com.dropdb.demo.service.CommentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {
    @Autowired
    CommentMapper commentMapper;


    /**
     * 获取所有评论
     * @param listCommentsBO
     * @return
     */
    @Override
    public BaseData ListComments(AdminListCommentsBO listCommentsBO) {

        CommentExample commentExample = new CommentExample();
        // 排序规则
        commentExample.setOrderByClause(listCommentsBO.getSort() + " " + listCommentsBO.getOrder());
        // 动态添加商品和用户id查询条件
        CommentExample.Criteria criteria = commentExample.createCriteria();
        // 如果userId不为空
        if (listCommentsBO.getUserId() != null) {
            criteria.andUserIdEqualTo(Integer.valueOf(listCommentsBO.getUserId()));
        }
        // 如果valueId不为空
        if (listCommentsBO.getValueId() != null) {
            criteria.andValueIdEqualTo(Integer.valueOf(listCommentsBO.getValueId()));
        }
        // 逻辑删除条件
        criteria.andDeletedEqualTo(false);
        // 添加分页
        PageHelper.startPage(listCommentsBO.getPage(), listCommentsBO.getLimit());
        List<Comment> commentList = commentMapper.selectByExample(commentExample);
        PageInfo<Comment> pageInfo = new PageInfo<>(commentList);
        long total = pageInfo.getTotal();
        return new BaseData(commentList, total);
    }


    /**
     * 删除评论
     * 逻辑删除
     * @param comment
     */
    /*@Override
    public void deleteComment(Comment comment) {
        comment.setDeleted(true);
        commentMapper.updateByPrimaryKey(comment);
    }*/

    /**
     * 删除评论
     * 物理删除
     */
    @Override
    public void deleteComment(Comment comment) {
        commentMapper.deleteByPrimaryKey(comment.getId());
    }



    //前台功能
    @Autowired
    JdbcTemplate jdbcTemplate;

    @Autowired
    CommentMultiTableMapper commentMultiTableMapper;

    /**
     * 获取评论条数
     * @param valueId
     * @param type
     * @return
     */
    @Transactional
    @Override
    public CommentCountData commentCount(Integer valueId, Byte type) {
        String allCommentSql = "select count(*) from `cskaoyanmall_comment` where `value_id` = ? and `type` = ?";
        String picCommentSql = "select count(*) from `cskaoyanmall_comment` where `value_id` = ? and `type` = ? and `has_picture` = 1";
        Object[] args = new Object[]{valueId, type};
        Integer allCount = jdbcTemplate.queryForObject(allCommentSql, args, Integer.class);
        Integer hasPicCount = jdbcTemplate.queryForObject(picCommentSql, args, Integer.class);
        return new CommentCountData(allCount, hasPicCount);
    }

    /**
     * 获取商品评论
     * @param valueId
     * @param type
     * @param size
     * @param page
     * @param showType 0全部，1有图
     * @return
     */
    @Override
    public CommentListData commentList(Integer valueId, Byte type, Integer size, Integer page, Integer showType) {
//        CommentExample commentExample = new CommentExample();
//        CommentExample.Criteria criteria = commentExample.createCriteria();
//        criteria
//                .andValueIdEqualTo(valueId)
//                .andTypeEqualTo(type);
//        if (1 == showType) {
//            criteria.andHasPictureEqualTo(true);
//        }
        PageHelper.startPage(page, size);
        List<CommentListElementData> list = commentMultiTableMapper.list(valueId, type, showType);
        long total = new PageInfo<>(list).getTotal();
        return new CommentListData(total, page, list);
    }




    /**
     * 专题评论
     * @param comment 评论内容
     * @param userId 用户id
     * @return
     */
    @Override
    public Integer submitTypeCommit(Comment comment, Integer userId) {
        // 添加用户id
        comment.setUserId(userId);
        // 添加更改时间
        Date date = new Date();
        comment.setAddTime(date);
        comment.setUpdateTime(date);
        // 图片信息
        if (comment.getPicUrls() != null) comment.setHasPicture(true);
        int insert = commentMapper.insertSelective(comment);
        return insert;
    }
}
